package com.liang.six_Direct_Exchanges;

import com.rabbitmq.client.*;
import utils.RabbitmqUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/18
 * Time: 17:40
 * Description:
 */
public class ReceiveLogs_Direct01 {

    //1 交换机的名称
    public static final String EXCHANGE_DECLARE_NAME = "direct_logs";


    public static void main(String[] args) throws IOException, TimeoutException {

        Channel channel = RabbitmqUtil.getChannel();



        //1 声明一个交换机

        channel.exchangeDeclare(EXCHANGE_DECLARE_NAME, BuiltinExchangeType.DIRECT);

        //2 声明一个临时队列  队列的名称是随机的 当消费者断开队列的连接的时候 队列就会自动删除


        String queue_name = "disk";
        String routingKey = "info";

        channel.queueDeclare(queue_name,false,false,false,null);


        // 交换机 绑定队列

        channel.queueBind(queue_name,EXCHANGE_DECLARE_NAME, routingKey);

        System.out.println("1等待接受消息....");

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                System.out.println("consumerTag = " + consumerTag+" message = "+ new String(message.getBody(), StandardCharsets.UTF_8));
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                System.out.println("消费者取消1 消费log:"+consumerTag);
            }
        };

        channel.basicConsume(queue_name,true,deliverCallback,cancelCallback);


    }


}